﻿using Eps.Ai.Common;
using InfonavaEye.components;
using InfonavaEye.handler;
using InfonavaEye.Handler;
using InfonavaEye.util;
using Sylan.Common.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.Forms
{
    public partial class SettingForm : Form
    {
        private CameraHandler cameraHandler = CameraHandler.Instance;
        private PLCHandler plcHandler = PLCHandler.Instance;
        private LightHandler lightHandler = LightHandler.Instance;

        public SettingForm()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            this.lblDeviceNo.Text = Config.DEVICE_SN;
            this.lblAppVersion.Text = Config.APP_VERSION;
            drawLine(this.panel1, RectLocation.TOP, Color.Red);//Color.FromArgb(31,187,187,187)
            drawLine(this.panel2, RectLocation.RIGHT, Color.FromArgb(31, 187, 187, 187));
            this.volumeTrackBar.Value = Config.AUDIO_VOLUME;
            this.lblVolume.Text = Config.AUDIO_VOLUME.ToString();
            this.calcUserTimes();
            this.cmbLanguage.SelectedIndex = 0;
        }

        private void calcUserTimes()
        {

            //软件首次使用日期
            if (String.IsNullOrEmpty(Config.APP_FIRST_USE_DATE))
            {
                string firstDate = DateTime.Now.ToString("yyyy-MM-dd");
                Config.APP_FIRST_USE_DATE = firstDate;
                Config.INI.WriteValue("device", "APP_FIRST_USE_DATE", firstDate);
            }

            int ocrUsedTimes = Config.OCR_USERD_TIMES;//OCR已经使用的次数
            DateTime firstUseDate = DateTime.Parse(Config.APP_FIRST_USE_DATE);//软件首次使用日期
            int useDays = DateTime.Now.Subtract(firstUseDate).Days;//已使用天数
            int saveSeconds = Config.APP_SAVE_SECONDS;//已节省时间（单位s）

            this.rtxtSummary.Text = string.Format("已为您提供服务{0} 天，共识别 {1} 次，节省约 {2} 小时 {3} 分钟", useDays, ocrUsedTimes, saveSeconds / 60 / 60, saveSeconds % (60 * 60) / 60);
            int textStart = 7;
            this.rtxtSummary.Select(textStart, useDays.ToString().Length + 1);
            this.rtxtSummary.SelectionColor = Color.FromArgb(255, 0, 77, 203);
            textStart += useDays.ToString().Length + 6;
            this.rtxtSummary.Select(textStart, ocrUsedTimes.ToString().Length + 1);
            this.rtxtSummary.SelectionColor = Color.FromArgb(255, 0, 77, 203);
            textStart += ocrUsedTimes.ToString().Length + 7;
            this.rtxtSummary.Select(textStart, 20);
            this.rtxtSummary.SelectionColor = Color.FromArgb(255, 0, 77, 203);
            this.rtxtSummary.Select(0, 0);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, panel1.ClientRectangle,
            Color.White, 1, ButtonBorderStyle.Solid, //左边
            Color.FromArgb(31, 187, 187, 187), 2, ButtonBorderStyle.Solid, //上边
            Color.White, 1, ButtonBorderStyle.Solid, //右边
            Color.White, 1, ButtonBorderStyle.Solid);//底边
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, panel2.ClientRectangle,
          Color.White, 1, ButtonBorderStyle.Solid, //左边
          Color.White, 1, ButtonBorderStyle.Solid, //上边
          Color.FromArgb(31, 187, 187, 187), 2, ButtonBorderStyle.Solid, //右边
          Color.White, 1, ButtonBorderStyle.Solid);//底边
        }

        private void drawLine(Control uc, RectLocation location, Color penColor)
        {
            //创建画布
            Graphics g = uc.CreateGraphics();
            //设置画笔颜色 画笔宽度
            Pen pen = new Pen(penColor, 10);

            //绘制两个端点（默认为right） 
            Point startPoint = new Point(uc.Location.X + uc.Width, uc.Location.Y);
            Point endPoint = new Point(uc.Location.X + uc.Width, uc.Location.Y + uc.Height);
            if (location == RectLocation.TOP)
            {
                //绘制两个端点
                startPoint = new Point(0, 1);
                endPoint = new Point(uc.Width, 1);
            }
            else if (location == RectLocation.BOTTOM)
            {
                //绘制两个端点 
                startPoint = new Point(uc.Location.X, uc.Location.Y + uc.Height);
                endPoint = new Point(uc.Location.X + uc.Width, uc.Location.Y + uc.Height);
            }
            else if (location == RectLocation.LEFT)
            {
                //绘制两个端点 
                startPoint = new Point(uc.Location.X, uc.Location.Y + uc.Height);
                endPoint = new Point(uc.Location.X + uc.Width, uc.Location.Y + uc.Height);
            }

            //用笔和端点绘制直线
            g.DrawLine(pen, startPoint, endPoint);
            g.Dispose();
        }

        private void tab1_Click(object sender, EventArgs e)
        {
            toggleTab(1);
        }

        private void tab2_Click(object sender, EventArgs e)
        {
            toggleTab(2);
        }

        private void toggleTab(int tabIndex)
        {
            if (tabIndex == 1)
            {
                lblTab1.ForeColor = Color.FromArgb(255, 0, 77, 203);
                lblTab2.ForeColor = Color.FromArgb(255, 16, 16, 16);
                tab1.BackColor = Color.FromArgb(255, 227, 233, 251);
                tab2.BackColor = Color.FromArgb(255, 255, 255, 255);
                tabContent1.Visible = true;
                tabContent2.Visible = false;
            }
            else
            {
                lblTab1.ForeColor = Color.FromArgb(255, 16, 16, 16);
                lblTab2.ForeColor = Color.FromArgb(255, 0, 77, 203);
                tab1.BackColor = Color.FromArgb(255, 255, 255, 255);
                tab2.BackColor = Color.FromArgb(255, 227, 233, 251);
                tabContent2.Visible = true;
                tabContent1.Visible = false;
            }
        }

        /// <summary>
        /// 恢复默认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDefault_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定恢复默认吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //1.光源恢复初始亮度
                lightHandler.ChangeLuminance(Config.LIGHT_SOURCE_LUMINOSITY_DEFAULT);

                //2.电机停止（所有的寄存器恢复到0）+ 电机回到机械原点
                ThreadHelper.ExcuteThreadTask(() =>
                {
                    plcHandler.stopMoving();
                    plcHandler.moveToOrigin();
                    AutoCloseMessageBox.Show("已恢复默认");
                });
            }
        }

        private void volumeTrackBar_Scroll(object sender, EventArgs e)
        {
            lblVolume.Text = volumeTrackBar.Value.ToString();
            Config.AUDIO_VOLUME = volumeTrackBar.Value;
            Config.INI.WriteValue("device", "AUDIO_VOLUME", Config.AUDIO_VOLUME.ToString());
            SoundHelper.SetVolume(Config.AUDIO_VOLUME);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出程序吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                System.Environment.Exit(0);
            }
        }
    }

    public enum RectLocation
    {
        TOP,
        BOTTOM,
        LEFT,
        RIGHT,
    }
}
